/**
 * 
 */
package com.jxc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jxc.dao.GoodsDaoI;
import com.jxc.model.po.Brand;
import com.jxc.model.po.Discount;
import com.jxc.model.po.Goods;
import com.jxc.model.po.GoodsType;
import com.jxc.model.po.Repertory;
import com.jxc.model.vo.VGoods;

/**
 * @author Tino
 * 
 */
@Repository("goodsDao")
public class GoodsDaoImpl implements GoodsDaoI {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public List<VGoods> combobox(VGoods vrw) {
		Session session = this.sessionFactory.openSession();
		List<VGoods> list = this.changeModelGoods(this.find(vrw, session));
		session.close();
		return list;
	}

	private List<VGoods> changeModelGoods(List<Goods> goodsz) {
		List<VGoods> vgoodsz = new ArrayList<VGoods>();
		if (goodsz != null && goodsz.size() > 0) {
			for (Goods goods : goodsz) {
				VGoods vgoods = new VGoods();
				BeanUtils.copyProperties(goods, vgoods);

				if (goods.getBrand() != null) {
					Brand brand = goods.getBrand();
					vgoods.setBid(brand.getId());
					vgoods.setBname(brand.getName());
				}
				if (goods.getGoodsType() != null) {
					GoodsType goodsType = goods.getGoodsType();
					vgoods.setGtid(goodsType.getId());
					vgoods.setGtname(goodsType.getName());
				}
				if (goods.getDiscount() != null) {
					Discount discount = goods.getDiscount();
					vgoods.setDid(discount.getId());
					vgoods.setDiscountNum(discount.getDiscountNum());
				}
				vgoodsz.add(vgoods);
			}
		}
		return vgoodsz;
	}

	private List<Goods> find(VGoods vgoods, Session session) {
		String hql = "from Goods t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vgoods, hql, values);
		Query q = session.createQuery(hql);
		if (values != null && values.size() > 0) {
			for (int i = 0; i < values.size(); i++) {
				q.setParameter(i, values.get(i));
			}
		}

		return q.list();
	}

	private String addWhere(VGoods vgoods, String hql, List<Object> values) {
		if (vgoods.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vgoods.getId());
		}
		if (vgoods.getBarcode() != null && !"".equals(vgoods.getBarcode().trim())) {
			hql += " and t.barcode like ? ";
			values.add("%%" + vgoods.getBarcode() + "%%");
		}
		if (vgoods.getType() != null && !"".equals(vgoods.getType().trim())) {
			hql += " and t.type like ? ";
			values.add("%%" + vgoods.getType() + "%%");
		}
		if (vgoods.getColor() != null && !"".equals(vgoods.getColor().trim())) {
			hql += " and t.color like ? ";
			values.add("%%" + vgoods.getColor() + "%%");
		}
		if (vgoods.getSize() != null && !"".equals(vgoods.getSize().trim())) {
			hql += " and t.size like ? ";
			values.add("%%" + vgoods.getSize() + "%%");
		}
		if (vgoods.getGtid() != null) {
			hql += " and t.goodsType.id  = ? ";
			values.add(vgoods.getGtid());
		}
		if (vgoods.getBid() != null) {
			hql += " and t.brand.id  = ? ";
			values.add(vgoods.getBid());
		}
		if (vgoods.getRid() != null) {
			hql += " and t.repertory.id = ? ";
			values.add(vgoods.getRid());
		}
		hql += " and t.isDelete = 0 ";
		return hql;
	}

}
